import 'package:flutter/cupertino.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/assets.dart';
import 'package:safetymonitor/app/core/ext/widget_extension.dart';
import 'package:safetymonitor/app/core/utils/user_manager.dart';
import 'package:safetymonitor/app/data/entities/auth/function_entity.dart';
import 'package:safetymonitor/generated/assets.dart';
import 'package:safetymonitor/router/app_pages.dart';
import 'user_control.dart';

/// description: 用户模块
class UserPage extends StatelessWidget {
  UserPage({super.key});

  final controller = Get.put(UserControl());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            backgroundColor: const Color(0xFFF7FBFE),
            body: Stack(
              children: [
                Image.asset(
                  'assets/images/safe_mine_top_bg.png',
                  width: 375.w,
                  height: 258.w,
                ),
                Container(
                  margin: EdgeInsets.only(top: 107.w),
                  child: Column(
                    children: [
                      SingleChildScrollView(
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 16.w),
                          child: Column(
                            children: [
                              // 基本信息
                              _getUserInfo(),
                              // 功能信息
                              _getFunctions(context),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          );
        });
  }

  /// 白色圆角背景
  Decoration getWhiteRoundedBg() {
    return BoxDecoration(
      borderRadius: BorderRadius.circular(10.w),
      color: Colors.white,
      boxShadow: [
        BoxShadow(
          color: const Color(0x140545C7),
          offset: Offset(0, 3.w),
          blurRadius: 4.w,
        ),
      ],
    );
  }

  /// 基本信息
  Widget _getUserInfo() {
    return Row(
      children: [
        AppGaps.hGap10,
        Container(
            width: 70.w,
            height: 70.w,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(50.w),
              color: Colors.white,
              border: Border.all(color: Colors.white, width: 2.w),
            ),
            child: ClipOval(child: controller.getAvatarPath().asImg(clickShowLarge: true, fit: BoxFit.fill, radius: 7.w))),
        AppGaps.hGap18,
        // 用户名称
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text(
                    UserManager.getUser()?.nickname ?? '',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  AppGaps.hGap14,
                  SvgPicture.asset(UserManager.getUser()?.sex == 1 ? Assets.svgIconMianGirl : Assets.svgIconMianBoy),
                ],
              ).clickOnce(controller.onTapSetting),
              AppGaps.vGap8,
              Text(
                '手机号码：${UserManager.getUser()?.mobile}',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 12.sp,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  /// 单个功能样式
  Widget _getFunctionItem(bool bool, String svgIcon, {required String title, required String titleContent, required String icon}) {
    return Container(
      padding: EdgeInsets.all(20.w),
      child: Row(
        children: [
          SvgPicture.asset(svgIcon),
          AppGaps.hGap14,
          Expanded(
            child: Text(
              title,
              style: TextStyle(
                color: const Color(0xFF1D3452),
                fontSize: 15.sp,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          !bool
              ? Icon(Icons.arrow_forward_ios, size: 12.sp)
              : Text(
                  titleContent,
                  style: TextStyle(
                    color: const Color(0xFF6C779C),
                    fontSize: 15.sp,
                    fontWeight: FontWeight.w400,
                  ),
                )
        ],
      ),
    );
  }

  /// 功能列表样式
  Widget _getFunctionList(List<FunctionEntity> functionList, bool bool) {
    return Container(
      margin: EdgeInsets.only(top: 24.w),
      decoration: getWhiteRoundedBg(),
      child: ListView.separated(
        padding: EdgeInsets.zero,
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemCount: functionList.length,
        separatorBuilder: (context, index) {
          return _getDivider();
        },
        itemBuilder: (context, index) {
          return _getFunctionItem(
            bool,
            functionList[index].svgIcon,
            title: functionList[index].title,
            icon: functionList[index].icon,
            titleContent: functionList[index].titleContent,
          ).clickOnce(() {
            functionList[index].onTap();
          });
        },
      ),
    );
  }

  ///获取分割线
  Widget _getDivider() {
    return Container(
      margin: EdgeInsets.only(left: 20.w, right: 20.w),
      child: Divider(
        height: 1.w,
        color: const Color(0xFFEAF2FA),
      ),
    );
  }

  /// 功能信息
  Widget _getFunctions(BuildContext context) {
    return Column(
      children: [
        _getFunctionList([
          FunctionEntity(title: '公司', icon: Assets.svgIconVersionInfo, svgIcon: Assets.svgIconSafeMineCpany, titleContent: UserManager.getUserCompanyName() ?? '', onTap: () {}),
          FunctionEntity(title: '部门', icon: Assets.svgIconHelp, svgIcon: Assets.svgIconSafeMineDepartment, titleContent: UserManager.getUser()?.dept.name ?? '', onTap: () {}),
          FunctionEntity(title: '岗位', icon: Assets.svgIconSetting, svgIcon: Assets.svgIconSafeMinePost, titleContent: controller.getPosts(), onTap: () {}),
          FunctionEntity(title: '角色', icon: Assets.svgIconSetting, svgIcon: Assets.svgIconSafeMineEducationalBackground, titleContent: controller.getRoles(), onTap: () {}),
          FunctionEntity(title: '邮箱', icon: Assets.svgIconSetting, svgIcon: Assets.svgIconSafeMinePostMajor, titleContent: UserManager.getUser()?.email ?? '', onTap: () {}),
          FunctionEntity(title: '版本更新', icon: Assets.svgIconSetting, svgIcon: Assets.imagesIconVersionUpdate, onTap: () {
            Get.toNamed(Routes.personalVersionInfo);
          }),
        ], true),
        _getFunctionList([
          // FunctionEntity(title: '切换用户', icon: Assets.svgIconSwitchUser, onTap: control.onTapSwitchUser),
          FunctionEntity(
              title: '退出登录',
              icon: Assets.svgIconExit,
              svgIcon: Assets.svgIconSafeMineLogout,
              onTap: () {
                showCupertinoModalPopup(
                  context: context,
                  builder: (context) {
                    return CupertinoActionSheet(
                      actions: [
                        CupertinoActionSheetAction(
                          child: const Text('退出登录'),
                          onPressed: () {
                            Navigator.of(context).pop();
                            UserManager.logout();
                          },
                        ),
                      ],
                      cancelButton: CupertinoActionSheetAction(
                        onPressed: Navigator.of(context).pop,
                        child: const Text('取消'),
                      ),
                    );
                  },
                );
              }),
        ], false),
      ],
    );
  }
}
